package com.henghai.zhenxuan.ui.page;

import static com.henghai.zhenxuan.data.config.Const.POSITION_ADOPT;
import static com.henghai.zhenxuan.data.config.Const.POSITION_HOME;
import static com.henghai.zhenxuan.data.config.Const.POSITION_MALL;
import static com.henghai.zhenxuan.data.config.Const.POSITION_MINE;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.henghai.zhenxuan.BR;
import com.henghai.zhenxuan.R;
import com.henghai.zhenxuan.data.config.AppConfig;
import com.henghai.zhenxuan.domain.event.Messages;
import com.henghai.zhenxuan.domain.message.PageMessenger;
import com.henghai.zhenxuan.domain.request.UserInfoRequester;
import com.henghai.zhenxuan.ui.page.adopt.AdoptFragment;
import com.henghai.zhenxuan.ui.page.mall.MallFragment;
import com.henghai.zhenxuan.ui.page.mine.MineFragment;
import com.henghai.zhenxuan.ui.page.raise.RaiseFragment;
import com.henghai.zhenxuan.utils.MainHandler;
import com.kongzue.dialogx.dialogs.TipDialog;
import com.kongzue.dialogx.dialogs.WaitDialog;
import com.kunminx.architecture.ui.page.BaseFragment;
import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.page.StateHolder;
import com.kunminx.architecture.ui.state.State;

public class MainFragment extends BaseFragment {

    private IndexStates mStates;
    private PageMessenger mMessenger;
    private UserInfoRequester mUserInfoRequester;
    private static final String TAG = "MainFragment";

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(IndexStates.class);
        mMessenger = getApplicationScopeViewModel(PageMessenger.class);
        mUserInfoRequester = getActivityScopeViewModel(UserInfoRequester.class);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mUserInfoRequester.getTokenResult().observe(this, dataResult -> {
            if (!dataResult.getResponseStatus().isSuccess()) {
                TipDialog.show(dataResult.getResponseStatus().getResponseCode(), WaitDialog.TYPE.ERROR);
                return;
            }
            Log.d(TAG, "onViewCreated: " + dataResult.getResult().getData());
            AppConfig.INSTANCE.getMConfigs().user().set(dataResult.getResult().getData());
            mMessenger.input(new Messages(Messages.EVENT_USERINFO_SUCCESS));
        });
        MainHandler.INSTANCE.postDelay(1000, () -> mUserInfoRequester.request(getViewLifecycleOwner()));
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.fragment_main, BR.vm, mStates);
    }

    public static class FragmentPagerAdapter extends FragmentStateAdapter {
        public FragmentPagerAdapter(@NonNull FragmentActivity fragmentActivity) {
            super(fragmentActivity);
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return getFragmentForType1(position);
        }

        //底部导航tab个数
        @Override
        public int getItemCount() {
            return 4;
        }

        private Fragment getFragmentForType1(int position) {
                Log.d(TAG, "getFragmentForType1: " + position);
            switch (position) {
                case POSITION_HOME:
                    return new RaiseFragment();
                case POSITION_ADOPT:
                    return new AdoptFragment();
                case POSITION_MALL:
                    return new MallFragment();
//                case POSITION_SIGN_IN:
//                    return new SignInFragment();
                default:
                    return new MineFragment();
            }
        }
    }

    public static class IndexStates extends StateHolder {

        public final State<String> appType = new State<>("1,2,3,4");

    }
}
